<template>
  <div class="upload-container">
    <ul v-show="urlList !== ''" class="el-upload-list el-upload-list--picture-card">
      <li v-for="(item,index) in urlList" :key="index" class="el-upload-list__item is-ready">
        <div class="filediv">
          <img class="el-upload-list__item-thumbnail" :src="item">
        </div>
        <span class="el-upload-list__item-actions">
          <span class="el-upload-list__item-delete" @click="rmImage(index, $event)">
            <i class="el-icon-delete" />
          </span>
        </span>
      </li>
    </ul>
    <el-upload
      v-show="urlList.length < maxcount"
      :file-list="fileList"
      :on-remove="removeList"
      :limit="maxcount"
      :on-exceed="handleExceed"
      :multiple="multiple"
      list-type="picture-card"
      action="''"
      class="image-uploader"
:show-file-list="false"
      :http-request="upload"
    >
      <i slot="default" class="el-icon-plus" />
      <div class="el-upload__text">{{ title }}</div>

    </el-upload>
  </div>
</template>

<script>
import OSS from 'ali-oss'
import {
  oss_accessKeyId,
  oss_accessKeySecret,
  oss_bucket,
  oss_host,
  oss_regionId
} from '@/api/config'
import { getFileSuffix } from '@/utils/common'
import { Loading } from 'element-ui'

export default {
  name: 'SingleImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    },
    maxcount: {
      type: Number,
      default: 1
    },
    multiple: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: ''
    },
    format: {
      type: String,
      default: ''
    },
    urls: {
      type: String,
      default: function() {
        return ''
      }
    }
  },
  data() {
    return {
      urlList: [],
      fileList: [], // 文件列表
      ossParameter: {
        region: oss_regionId,
        accessKeyId: oss_accessKeyId,
        accessKeySecret: oss_accessKeySecret,
        bucket: oss_bucket,
        host: oss_host
      },
      loadingId: null
    }
  },
  computed: {

  },
  watch: {
    urls(newVal, oldVal) {
      if (newVal) {
        this.showImage()
      }
      true
    }
  },
  mounted() {
    this.showImage()
  },
  methods: {
    removeList(response, file, pop) {

    },
    handleExceed(response, file) {
      if (response.length > this.maxcount || (file.length + response.length) > this.maxcount) {
        this.$message.error('最多只能上传' + this.maxcount + '个')
        return false
      }
    },
    showImage() {
      if (this.urls !== undefined && this.urls !== '') {
        this.urlList = this.urls.split(',')
        if (this.urlList[this.urlList.length - 1] === undefined) {
          delete this.urlList[this.urlList.length - 1]
        }
      }
    },
    rmImage(index, e) {
      // delete this.urlList[index]
      e.stopPropagation()
      this.urlList.splice(index, 1)
      this.fileList.splice(index, 1)
      this.$emit('getImage', this.urlList.join(','))
    },
    upload(item) {
      console.log(item)
      let fileSuffix = getFileSuffix(item.file)
      fileSuffix = fileSuffix.toLowerCase()
      var formatList
      if (this.format !== '') {
        formatList = this.format.split(',')
        if (formatList.indexOf(fileSuffix) < 0) {
          this.$message.error('只能上传' + this.format + '格式文件')
          return
        }
      }
      this.loadingId = Loading.service({ fullscreen: true, text: '上传中，请稍等...' })
      const that = this
      const client = new OSS(this.ossParameter)
      const date = new Date()
      let filename = '/images/' + date.getTime() + '/' + item.file.name
      filename = filename.replace(/,/g,"");
      console.log('upload:' + that.urlList.join(','))
      // if (that.urlList.indexOf(that.ossParameter.host + filename) >= 0) {
      //   this.$message.error(that.ossParameter.host + filename + '已上传')
      //   that.loadingId.close()
      //   return
      // }
      client.put(filename, item.file).then(
        response => {
          const url = that.ossParameter.host + filename
          // this.urlList = this.urls.split(',')
          that.urlList.push(url)
          that.fileList.push(item.file)
          that.$emit('getImage', that.urlList.join(','))
          that.loadingId.close()
        }
      ).catch(err => {
        console.log('err:', err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import "src/styles/mixin.scss";

  .upload-container {
    /*width: 100%;*/
    display: flex;
    // justify-content: space-between;
    // align-items: flex-start;
    position: relative;
    @include clearfix;

    .image-uploader {
      float: left;
    }

    .image-preview {
      width: auto;
      height: 200px;
      display: flex;
      flex-direction: row;
      position: relative;
      border: 1px dashed #d9d9d9;
      float: left;
      margin-left: 50px;

      .image-preview-wrapper {
        position: relative;
        width: auto;
        // margin-left:10px ;
        height: 100%;

        img {
          height: 100%;
        }
      }

      .image-preview-action {
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        cursor: default;
        text-align: center;
        color: #fff;
        opacity: 0;
        font-size: 20px;
        background-color: rgba(0, 0, 0, .5);
        transition: opacity .3s;
        cursor: pointer;
        text-align: center;
        line-height: 200px;

        .el-icon-delete {
          font-size: 36px;
        }
      }

      &:hover {
        .image-preview-action {
          opacity: 1;
        }
      }
    }

    .image-app-preview {
      width: auto;
      height: 180px;
      position: relative;
      border: 1px dashed #d9d9d9;
      float: left;
      margin-left: 50px;

      .app-fake-conver {
        height: 44px;
        position: absolute;
        width: 100%; // background: rgba(0, 0, 0, .1);
        text-align: center;
        line-height: 64px;
        color: #fff;
      }
    }
  }

  .el-upload-list__item div.filediv {
    width: 100%;
    height: 100%;
  }

  ::v-deep .el-upload-list__item div.filediv img {
    width: 100%;
    max-width: 100%;
  }

  ::v-deep .el-upload__text {
    line-height: 30px;
  }
</style>
